package com.zj.osm.view.map.map

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import com.zj.osm.App
import com.zj.osm.R
import com.zj.osm.entity.db.ProgramTabDao
import com.zj.osm.entity.greendao.Point
import com.zj.osm.entity.greendao.ProgramTab
import com.zj.osm.utils.Configuration.ProgramAdpaterConfit
import com.zj.osm.view.BaseActivity
import com.zj.osm.view.project.addproject.ProgramAdpater
import com.zj.utils.data.LiveDataBus
import com.zj.utils.utils.utilcode.ToastUtils
import kotlinx.android.synthetic.main.activity_cable_well_problem_page.*
import java.util.*

/**
 * ┌─────────────────────────────────────────────────────────────┐
 * │┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐│
 * ││Esc│!1 │@2 │#3 │$4 │%5 │^6 │&7 │*8 │(9 │)0 │_- │+= │|\ │`~ ││
 * │├───┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴───┤│
 * ││ Tab │ Q │ W │ E │ R │ T │ Y │ U │ I │ O │ P │{[ │}] │ BS  ││
 * │├─────┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴─────┤│
 * ││ Ctrl │ A │ S │ D │ F │ G │ H │ J │ K │ L │: ;│" '│ Enter  ││
 * │├──────┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴────┬───┤│
 * ││ Shift  │ Z │ X │ C │ V │ B │ N │ M │< ,│> .│? /│Shift │Fn ││
 * │└─────┬──┴┬──┴──┬┴───┴───┴───┴───┴───┴──┬┴───┴┬──┴┬─────┴───┘│
 * │      │Fn │ Alt │         Space         │ Alt │Win│   HHKB   │
 * │      └───┴─────┴───────────────────────┴─────┴───┘          │
 * └─────────────────────────────────────────────────────────────┘
 * 版权：渤海新能 版权所有
 *
 * @author feiWang
 * 版本：1.5
 * 创建日期：12/23/20
 * 描述：OsmDroid
 * E-mail : 1276998208@qq.com
 * CSDN:https://blog.csdn.net/m0_37667770/article
 * GitHub:https://github.com/luhenchang
 */
class CableWellProblemPageActivity : BaseActivity() {
    private var pointID: Long? = 0L
    private lateinit var flag:String
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_cable_well_problem_page)
        //电缆井问题页面
        val bundle = intent.getBundleExtra("intent_bundle")
        pointID = bundle?.getLong("pointId")
        flag= bundle?.getString("flag")!!
        LiveDataBus.get().getChannel(ProgramAdpaterConfit, Long::class.java).observe(this, androidx.lifecycle.Observer<Long> {
            App.getInstances().daoSession.programTabDao.deleteByKey(it)
            initData()
        })
        initData()
        initView()


    }

    //UI数据展示
    private fun initView() {
        cw_pr_ok.setOnClickListener { finish() }
        flag


        val arrayList: Array<String>
        //性质和资源性质
        //性质和资源性质
        arrayList = if (flag == "电缆井") {
            resources.getStringArray(R.array.programType)
        } else { //环网箱
            resources.getStringArray(R.array.cableWellProblemType)
        }
        //适配器
        //适配器
        val srcArrayAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, arrayList)
        //设置样式
        //设置样式
        srcArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        cw_pr_name_sp.adapter = srcArrayAdapter
        cw_pr_name_sp.setSelection(0, true)
        cw_pr_name_sp.onItemSelectedListener=object :AdapterView.OnItemSelectedListener{
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                if (0 != position){
                    val name=cw_pr_name_sp.selectedItem as String
                    //进行增加操作
                    if (pointID != null&& name.isNotEmpty()) {
                        val programTabls:List<ProgramTab> = App.getInstances().daoSession.programTabDao.queryBuilder().where(ProgramTabDao.Properties.PointID.eq(pointID), ProgramTabDao.Properties.PrName.eq(name)).build().list()
                        if (programTabls.isNullOrEmpty()){
                            val programTab = ProgramTab()
                            programTab.id = null
                            programTab.pointID = pointID!!
                            programTab.prName=name
                            App.getInstances().daoSession.programTabDao.insert(programTab)
                            ToastUtils.showLong("新增成功")
                            initData()
                        }else{
                            ToastUtils.showLong("该问题已经添加")
                        }

                    }else{
                        ToastUtils.showLong("请填写完成所有数据")
                    }
                }
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }

        }

    }
    lateinit var programAdapter:ProgramAdpater
    //加载数据
    private fun initData() {
        val programTabls:List<ProgramTab> = App.getInstances().daoSession.programTabDao.queryBuilder().where(ProgramTabDao.Properties.PointID.eq(pointID)).build().list()
        programAdapter=ProgramAdpater(programTabls, this)
        recyclerViewAllProblem.adapter= programAdapter
        recyclerViewAllProblem.layoutManager=LinearLayoutManager(this)
        programAdapter.notifyDataSetChanged()
    }

    /**
     * 获取UUID
     *
     * @return uuid
     */
    private fun getUUID(): String? {
        return UUID.randomUUID().toString() + ""
    }


}
